﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor.ProjectWindowCallback;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;

using UnityEditor;
#endif 

// **********************************************************************
// Copyright (C) XM
// Author: 赵思佳
// Date: 2019-01-04
// Desc: 
// **********************************************************************
namespace SpaceDFramework
{
    public class EditorUtil
    {
#if UNITY_EDITOR
        /// <summary>
        /// 文件、文件夹打开类
        /// </summary>
        public class FileOpener
        {
            /// <summary>
            /// 打开
            /// </summary>
            /// <param name="url">地址</param>
            public static void OpenURL(string url)
            {
                Application.OpenURL("file://" + url);
            }
            /// <summary>
            /// 用特定地址打开
            /// </summary>
            /// <param name="exepath"></param>
            /// <param name="file"></param>
            public static void OpenByExe(string exepath, string file)
            {
                System.Diagnostics.Process.Start(exepath, file);
            }
        }
        public class CreateScriptAsset : EndNameEditAction
        {
            public override void Action(int instanceId, string pathName, string resourceFile)
            {
                UnityEngine.Object o = CreateScriptAssetFromTemplate(pathName, resourceFile);
                ProjectWindowUtil.ShowCreatedAsset(o);
            }

            internal static UnityEngine.Object CreateScriptAssetFromTemplate(string pathName, string resourceFile)
            {
                string fullPath = Path.GetFullPath(pathName);
                StreamReader streamReader = new StreamReader(resourceFile);
                string text = streamReader.ReadToEnd();
                streamReader.Close();

                //TODO:脚本内容处理
                string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName);
                text = Regex.Replace(text, "#SCRIPTNAME#", fileNameWithoutExtension);
                text = Regex.Replace(text, "#DATE#", System.DateTime.Now.ToString("yyyy-MM-dd"));

                bool encoderShouldEmitUTF8Identifier = true;
                bool throwOnInvalidBytes = false;
                UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes);
                bool append = false;
                StreamWriter streamWriter = new StreamWriter(fullPath, append, encoding);
                streamWriter.Write(text);
                streamWriter.Close();
                AssetDatabase.ImportAsset(pathName);
                return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object));
            }
        }
        /// <summary>
        /// 获取选中路径
        /// </summary>
        /// <returns></returns>
        public static string GetSelectedPathOrFallback()
        {
            string path = "Assets";
            foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
            {
                path = AssetDatabase.GetAssetPath(obj);
                if (!string.IsNullOrEmpty(path) && File.Exists(path))
                {
                    path = Path.GetDirectoryName(path);
                    break;
                }
            }
            return path;
        }
#endif
    }
}
